package com.kh.mychart;

import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kh.mychart.R;
import com.lovely3x.view.HeightView;
import com.lxj.xpopup.XPopup;

import java.math.BigDecimal;
import java.util.List;


public class ChartLineBarAdapter extends RecyclerView.Adapter<ChartLineBarAdapter.Holder> {

    List<SparseArray<MonthValue>> data;
    View content;
    HeightView hv_y;
    TextView tv_value;

    public static ChartLineBarAdapter create(List<SparseArray<MonthValue>> data, View content) {
        ChartLineBarAdapter adapter = new ChartLineBarAdapter();
        adapter.data = data;
        adapter.content = content;
        adapter.hv_y = content.findViewById(R.id.hv_y);
        adapter.tv_value = content.findViewById(R.id.tv_value);
        return adapter;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View content = View.inflate(parent.getContext(), R.layout.item, null);
        Holder holder = new Holder(content);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;//fixme 为什么View 会错乱？
    }

    public void update(List<SparseArray<MonthValue>> list) {
        data = list;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        SparseArray<MonthValue> map = data.get(position);
        System.out.println(position);
        MonthValue.Point point = map.get(1).current;
        holder.tv_date.setText(point.labelText);//1 就是当前年所放的位置
        holder.chart_content.map = map;
        final XPopup.Builder builder = new XPopup.Builder(holder.itemView.getContext()).watchView(holder.itemView);
        holder.itemView.setOnLongClickListener((View v) -> {
            int size = map.size();
            String[] labelList = new String[size];
            for (int i = 0; i < size; i++) {
                MonthValue.Point currentPoint = map.get(i + 1).current;
                labelList[i] = currentPoint.labelText + ": " + (int) currentPoint.real + " 件";
            }
            builder.asAttachList(labelList, null, 0, 10,
                    (int index, String text) -> {
                        if (0 == index) return;
                        Toast.makeText(v.getContext(),
                                text + "\n" + "可以钻去",
                                Toast.LENGTH_SHORT).show();
                    }
            ).show();
            return false;//false 执行点击事件
            //true 点击事件不执行
        });
        holder.itemView.setOnClickListener((View v) -> {
            Float value = point.real;
            int res = new BigDecimal(value.toString())
                    .divide(new BigDecimal("5"), 0, BigDecimal.ROUND_HALF_UP)
                    .intValue();
            hv_y.setCurrentLineIndex(res);
            tv_value.setText(String.format("日期: %s\n值: %.2f", point.labelText, point.real));
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    final class Holder extends RecyclerView.ViewHolder {
        TextView tv_date;
        LineBarChart chart_content;
        XPopup.Builder builder;

        public Holder(@NonNull View itemView) {
            super(itemView);
            tv_date = itemView.findViewById(R.id.tv_date);
            chart_content = itemView.findViewById(R.id.chart_content);
            builder = new XPopup.Builder(itemView.getContext()).watchView(itemView);
        }

    }
}
